﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;

public partial class Templates_ListSection_Default : BaseTemplatePage
{

    protected override void AttachEvents()
    {
        ucFeedModule.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.Post>>(ucFeedModule_SelectData);
        ucExploreModule.DataRequired += new EventHandler(ucExploreModule_DataRequired);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataBind();
        }

    }

    void ucExploreModule_DataRequired(object sender, EventArgs e)
    {
        ucExploreModule.DataSource = GetExploreData();
    }

    private List<Post> feedData;
    private List<Post> GetFeedData()
    {
        if (feedData == null)
        {
            OrganiseChildren();
        }
        return feedData;
    }

    private List<Post> exploreData;
    private List<Post> GetExploreData()
    {
        if (exploreData == null)
        {
            OrganiseChildren();
        }
        return exploreData;
    }

    private void OrganiseChildren()
    {
        if (AcmsContext.CurrentPost != null)
        {
            feedData = new List<Post>();
            exploreData = new List<Post>();
            foreach (Post p in AcmsContext.CurrentPost.Children)
            {
                if (p.TemplateId == AcmsContext.CurrentPost.TemplateId)
                {
                    exploreData.Add(p);
                }
                else
                {
                    feedData.Add(p);
                }
            }
        }
        else
        {
            exploreData = DummyPost.CreateDummyList(5);
            feedData = DummyPost.CreateDummyList(10);
        }
    }

    void ucFeedModule_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    {
        e.SetData(GetFeedData());
    }

}
